<%@page import="org.lxy.service.UserInfoService"%>
<%@page import="org.lxy.domain.UserInfoEntity"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="cn.hutool.core.bean.BeanUtil" %>
<%@ page import="org.lxy.domain.constant.CoreConstant" %>
<%@ page import="org.lxy.config.UserInfoConfig" %>
<%
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	WebApplicationContext context= WebApplicationContextUtils.getWebApplicationContext(application);
	UserInfoService userInfoService = context.getBean(UserInfoService.class);
	List<UserInfoEntity> list = userInfoService
			.listByMap(BeanUtil.beanToMap(UserInfoEntity.builder().username(username).password(password).build(),true,true));
	if (list == null || list.isEmpty()) {
		out.write("<script>alert('用户名或密码错误！');window.location.href='/login'</script>");
	} else {
		String s = UUID.randomUUID().toString();
		Cookie cookie = new Cookie(CoreConstant.USER_COOKIE, s);
		cookie.setMaxAge(60*60*24);
		response.addCookie(cookie);
		UserInfoConfig.GLOBAL_USER_INFO.put(s, list.get(0));
		request.getSession().setAttribute("Account", list.get(0));
		if (list.get(0).getRole().equals("管理员")) {
			response.sendRedirect("/index");
		} else {
			response.sendRedirect("/home");
		}
	}
%>